<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button
            type="primary"
            size="small"
            @click="addClick(1,'0')"
          >添加权限</el-button>
        </div>
        <!-- el-table渲染成树状的条件：1.数据中有children字段，2.el-table加row-key这个唯一标识 -->
        <el-table
          border
          :data="permissonList"
          row-key="id"
        >
          <el-table-column
            label="名称"
            prop="name"
          />
          <el-table-column
            label="标识"
            prop="code"
          />
          <el-table-column
            label="描述"
            prop="description"
          />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button
                v-if="row.type === 1"
                size="small"
                type="text"
                @click="addClick(2,row.id)"
              >添加权限点</el-button>
              <el-button
                size="small"
                type="text"
                @click="editPermission(row.id)"
              >查看</el-button>
              <el-button
                size="small"
                type="text"
                @click="delPermission(row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 新增编辑的弹层 -->
      <el-dialog
        :visible="showDialog"
        :title="showTitle"
        @close="showDialog = false"
      >
        <!-- 表单内容 -->
        <el-form
          ref="addForm"
          label-width="100px"
          :model="formData"
          :rules="rules"
        >
          <el-form-item
            label="权限名称"
            prop="name"
          >
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item
            label="权限标识"
            prop="code"
          >
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item
            label="权限描述"
            prop="description"
          >
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-text="启用"
              active-value="1"
              inactive-text="不启用"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="showDialog = false">取消</el-button>
            <el-button
              type="primary"
              @click="addPermission"
            >确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermissonListApi } from '@/api/permisson'
import { tansListToTreeData } from '@/utils/index'
import {
  addPermissionAPi,
  delPermissionApi,
  editPermissionAPi,
  getPermissionDetailApi
} from '@/api/permisson'
export default {
  name: 'Permission',
  data() {
    return {
      permissonList: [],
      showDialog: false,
      formData: {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      },
      rules: {
        name: [{ required: true, message: '名称不可为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不可为空', trigger: 'blur' }],
        description: [{ required: true, message: '描述不可为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '编辑权限' : '新增权限'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const { data } = await getPermissonListApi()
      // console.log(data)
      this.permissonList = tansListToTreeData(data, '0')
      // console.log(this.permissonList)
    },
    closeDialog() {
      this.showDialog = false
      this.$refs.addForm.resetFields()
      this.formData = {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      }
    },
    addClick(type, pid) {
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },
    addPermission() {
      this.$refs.addForm.validate(async flag => {
        if (!flag) return
        if (this.formData.name === '' || this.formData.code === '') {
          this.$message.error('权限名称和权限标识不能为空')
          return
        }
        if (this.formData.id) {
          await editPermissionAPi(this.formData)
          this.$message.success('修改成功')
        } else {
          await addPermissionAPi(this.formData)
          this.$message.success('添加成功')
        }
        this.closeDialog()
        this.getPermissionList()
      })
    },
    delPermission(id) {
      this.$confirm('请问确定要删除吗', '温馨提示')
        .then(async() => {
          await delPermissionApi(id)
          this.$message.success('删除成功')
          this.getPermissionList()
        })
        .catch(() => {})
    },
    async editPermission(id) {
      const { data } = await getPermissionDetailApi(id)
      // console.log(data)//data里有id
      this.formData = data
      this.showDialog = true
    }
  }
}
</script>
